package com.catbad.services.impl;

import com.catbad.domain.Area;
import com.catbad.domain.Cinema;
import com.catbad.domain.User;
import com.catbad.mapper.AreaMapper;
import com.catbad.mapper.CinemaMapper;
import com.catbad.mapper.UserMapper;
import com.catbad.services.CinemaServices;
import com.catbad.util.Tools;
import com.catbad.vo.CinemaPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CinemaServicesImpl implements CinemaServices {
    @Autowired
    CinemaMapper cinemaMapper;
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    //事务处理
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public int saveCinema(Cinema cinema) {
        //判断数据是否为空
        if(     cinema.getCname()==null||"".equals(cinema.getCname())||
                cinema.getAid()==null||"".equals(cinema.getAid())||
                cinema.getCaddress()==null||"".equals(cinema.getCaddress())||
                cinema.getDescription()==null||"".equals(cinema.getDescription())||
                cinema.getCphonenum()==null||"".equals(cinema.getCphonenum())||
                cinema.getCaccount()==null||"".equals(cinema.getCaccount())
        ){
                return 3;
        }
        //判断是否影院重名
        if(cinemaMapper.selectCinemaByName(cinema.getCname())!=null){
                return 2;
        }//判断是否影院账号重名
        if(cinemaMapper.selectCinemaByCaccount(cinema.getCaccount())!=null){
            return 4;
        }
        //存储影院
        int code = cinemaMapper.saveCinema(cinema);
        //存储影院负责人账号
        User user = new User();
        try {
            user.setUaccount(cinema.getCaccount());
            user.setUname(cinema.getCname());
            user.setUpass(Tools.getMd5("111"));
            user.setSex("1");
            user.setRid(2L);
            user.setUimg("ht.img");
            user.setUdeleteFlag("1");
            user.setAge(18);
            user.setPhonenum(cinema.getCphonenum());
            user.setMail("mail@qq.com");
        } catch (Exception e) {
            e.printStackTrace();
        }
        userMapper.save(user);
        //返回结果
        return code;
    }

    @Override
    public CinemaPageVO findPageForCinema(Integer aid1,Integer aid2, String cname, Integer pageNO, Integer rows) {
        //保留过滤调试
        //第一次进入都为null
        Map<String,Object> params = new HashMap<>();
        //cname
        if(cname==null){
            params.put("cname","");
        }else {
            params.put("cname",cname);
        }
        //aid1
        if(aid1==null){
            params.put("aid1","");
        }else {
            params.put("aid1",aid1);
        }
        //aid2
        if(aid2==null){
            params.put("aid2","");
        }else {
            params.put("aid2",aid2);
        }

        //处理分页元素
        //如果是第一次进入则都为null
        //参数有 当前页码， 一页行数，总页数，
        //手动输入页码可能会超过上限和下限
        if(pageNO==null){
            pageNO=1;
        }
        if(pageNO<1){
            pageNO=1;
        }
        //计算出查询条件下的总行数，用于前端页面，展示总页数使用
        long total = cinemaMapper.total(params);
        if(rows==null){
            rows=4;
        }
        int maxPage = (int)(total%rows==0? total/rows:(total/rows+1));
        //有可能等于0，
        maxPage = Math.max(1,maxPage);
        if(pageNO>maxPage){
            pageNO=maxPage;
        }

        //sql分页需要 limit start 和length
        int start = rows*(pageNO-1);
        int length = rows;
        params.put("start",start);
        params.put("length",length);
        List<Map<String,Object>> cinemaList= cinemaMapper.findList(params);
        List<Area> areaList = areaMapper.selectArea1();
        CinemaPageVO cinemaPageVO = new CinemaPageVO();
        //赋值
        cinemaPageVO.setPageNo(pageNO);
        cinemaPageVO.setMaxPage(maxPage);
        cinemaPageVO.setRows(rows);
        cinemaPageVO.setTotal(total);

        cinemaPageVO.setParams(params);

        cinemaPageVO.setAreaList(areaList);
        cinemaPageVO.setCinemaList(cinemaList);

        return cinemaPageVO;
    }

    @Override
    public int deleteCinema(Long cid) {
       return cinemaMapper.deleteByPrimaryKey(cid);
    }

    @Override
    public Map<String, Object> selectForSet(Long cid) {
        return cinemaMapper.selectForSet(cid);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public int setCinema(Cinema cinema) {
        cinema.setCdeleteFlag("1");
        User user = userMapper.selectByName(cinema.getCname());
        user.setUaccount(cinema.getCaccount());
        userMapper.updateByPrimaryKey(user);
        return cinemaMapper.updateByPrimaryKey(cinema);
    }

    @Override
    public List<Cinema> selectCinemaByAid(Long aid) {
        return cinemaMapper.selectCinemaByAid(aid);
    }

    @Override
    public Cinema getCinemaById(Long cid) {
        return cinemaMapper.selectByPrimaryKey(cid);
    }
}
